
var Main = require("Main");
var Root = require("Root");
cc.Class({
    extends: cc.Component,

    properties: {

    },



    onLoad() {
        this.mousePos0 = null;//初始位置
        this.out = false;//用于判断物体是否移除
        this.isDrag = false;//判断物体是否是拖拽状态
        this.only = true;//拖拽过程中 只判断一次物体状态
        this.puzzleBG = [];//拼图背景组成的数组
        this.disArray = [];//移出后松手时，每个背景距离物体的距离组成的数组
        this.disArrayFriend = [];
        this.main = cc.find("Canvas").getComponent(Main);
        this.startpos = this.main.startpos;//所有物体的初始位置组成的数组

        // this.isHave = this.main.isHave;//关联脚本
        this.startpuzzle = this.main.startpuzzle;
        this.BGisHave = this.main.BGisHave;
        this.index = 1;//索引，用于判断松手后，物体的位置
        this.indexFriend = 1;
        this.minIndex = 0;
        this.minIndexFriend = 0;
        this.once = true;
        this.haveBig = false;
        this.isfind = false;
        this.letgo = true;
        this.width = this.main.column;
        this.height = this.main.row;
        this.dontTwoEnd = false;
        // this.useless = this.main.useless;
        this.name = "";/*this.node.getChildByName("New Label").getComponent(cc.Label).string*/;
        this.BGW = 0;
        this.BGH = 0;
        this.BGWFriend = 0;
        this.BGHFriend = 0;
        this.distance = 0;
        this.poschangedelaytime = 0;

        this.AllCorrectArray = this.main.AllCorrectArray;
        //  this.LockArray = this.main.LockArray;
        // this.isLock = this.main.isLock;
        //   console.log(this.isLock);
        //this.puzzleBG2 = [];/* this.main.puzzleBG2;*/
        this.correctMusic = this.main.puzzleBGNode.getComponent(cc.AudioSource);
        this.moveMusic = this.main.movePart.getComponent(cc.AudioSource);

        this.totalCount = this.main.row * this.main.column + this.main.useless;





        // console.log(this.anim.position);
        //this.mask = this.main.mask;
        // this.repeatLabel = this.main.repeatLabel;
        // this.repeatButton = this.main.repeatButton;
        // this.winLabel = this.main.winLabel;

        //this.currentzIndex = this.node.zIndex + 1;


        //  console.log(this.correctMusic);


        var self = this;
        // var puzzlebgChildren = this.main.puzzleBGNode.getChildren();
        // for (let i = 0; i < puzzlebgChildren.length; i++) {
        //     this.puzzleBG.push(puzzlebgChildren[i]);

        // }
        // console.log(this.puzzleBG);




        this.node.on(cc.Node.EventType.TOUCH_START, function (event) {

            // console.log("点击")
            // console.log(self.main.puzzleBG2);
            //(this);
            //console.log(self.totalCount);
            console.log(this.name);
            self.dontTwoEnd = true;
            //  console.log(self.startpuzzle);
            //  console.log(self.startpos);
            // console.log("变了");


            if (this.y > 40) {
                if (self.main.donContOnMask.parent != self.main.content) {
                    self.main.content.addChild(self.main.donContOnMask);
                }

            }
            else {
                //  console.log("小于40");
                if (self.main.donContOnMask.parent != self.main.content) {

                    self.main.content.addChild(self.main.donContOnMask);
                    self.main.donContOnMask.setSiblingIndex(self.main.content.childrenCount - 1);
                }

            }

            // if (this.getSiblingIndex() == 0) {
            //     self.main.dontOnMask.setSiblingIndex(0);
            // }
            // else {
            //     self.main.dontOnMask.setSiblingIndex(this.getSiblingIndex() - 1);
            // }

            self.mousePos0 = event.getLocation();
            // console.log(self.mousePos0);
            let clickIndex = -1;

            for (let i = 0; i < self.AllCorrectArray.length; i++) {
                if (self.AllCorrectArray[i].indexOf(this) != -1) {
                    clickIndex = i;
                    //   console.log("有兄弟");
                    break;
                }
                else {
                    clickIndex = -1;
                }
            }
            if (clickIndex != -1) {
                //  console.log("不等");
                for (let i = 0; i < cc.animPos[0].length; i++) {
                    for (let j = 0; j < self.AllCorrectArray[clickIndex].length; j++) {

                        if (cc.animPos[0][i].bg.indexOf(self.AllCorrectArray[clickIndex][j].name) != -1) {
                            if (self.main.animNum.indexOf(i) == -1) {
                                self.main.animNum.push(i);
                            }

                            //    console.log("赋个新的");
                            // console.log(self.main.animNum);
                            break;
                        }

                    }



                }
            }
            else {
                //  console.log("单崩");
                for (let i = 0; i < cc.animPos[0].length; i++) {
                    if (cc.animPos[0][i].bg.indexOf(this.name) != -1) {
                        //  console.log(self.main.animNum.indexOf(this.name));

                        if (self.main.animNum.indexOf(i) == -1) {
                            self.main.animNum.push(i);
                            break;
                        }

                    }



                }
            }

            // console.log("self.main.animNum=" + self.main.animNum);
            // console.log(self.main.animNum);
            if (self.main.animNum.length > 0) {
                //  console.log(self.main.animNum);
                //    console.log(self.AllCorrectArray);

                //  console.log(this);
                // console.log(cc.animPos[0][self.main.animNum[i]].animActive);
                //  console.log(cc.animPos[0][self.main.animNum[i]].bg.indexOf(this.name.toString()));
                for (let i = 0; i < self.main.animNum.length; i++) {
                    if (cc.animPos[0][self.main.animNum[i]].animActive && cc.animPos[0][self.main.animNum[i]].bg.indexOf(this.name.toString()) != -1) {
                        //console.log("进来了吧");
                        //  console.log(cc.animPos[0][self.main.animNum[i]].animPrefab);
                        cc.animPos[0][self.main.animNum[i]].animPrefab.parent = this;

                        let offsetx = cc.animPos[0][self.main.animNum[i]].x - self.main.puzzleBGNode.getChildByName(self.name).x;
                        let offsety = cc.animPos[0][self.main.animNum[i]].y - self.main.puzzleBGNode.getChildByName(self.name).y;

                        cc.animPos[0][self.main.animNum[i]].animPrefab.setPosition(cc.v2(offsetx, offsety));

                    }
                }

            }

            let find2 = false;

            for (let i = 0; i < self.AllCorrectArray.length; i++) {
                let haveAnim2 = [];
                if (self.AllCorrectArray[i].indexOf(this) != -1) {
                    for (let j = 0; j < self.AllCorrectArray[i].length; j++) {
                        if (self.AllCorrectArray[i][j].getComponentInChildren(sp.Skeleton) != null) {
                            haveAnim2.push(self.AllCorrectArray[i][j]);
                            continue;
                        }

                        self.AllCorrectArray[i][j].setSiblingIndex(self.totalCount);


                        if (self.main.dontOnMask.parent != self.main.movePart) {
                            self.main.movePart.addChild(self.main.dontOnMask);
                            self.main.dontOnMask.zIndex = 1000;
                        }

                    }
                    if (haveAnim2.length != 0) {
                        for (let index = 0; index < haveAnim2.length; index++) {
                            haveAnim2[index].setSiblingIndex(self.totalCount);

                        }

                    }


                    find2 = true;
                    break;
                }
            }

            if (!find2) {

                this.setSiblingIndex(self.totalCount);
                if (self.main.dontOnMask.parent != self.main.movePart) {
                    self.main.movePart.addChild(self.main.dontOnMask);
                    self.main.dontOnMask.zIndex = 1000;
                }


            }
            let blackIndex = -1;
            for (let i = 0; i < self.AllCorrectArray.length; i++) {
                if (self.AllCorrectArray[i].indexOf(this) != -1) {
                    blackIndex = i;
                    break;
                }
            }
            if (blackIndex != -1) {
                // let minIndex = 0;
                let minNode = self.AllCorrectArray[blackIndex][0];
                for (let j = 1; j < self.AllCorrectArray[blackIndex].length; j++) {
                    if (self.AllCorrectArray[blackIndex][j].getSiblingIndex() < minNode.getSiblingIndex()) {
                        //minIndex = j;
                        minNode = self.AllCorrectArray[blackIndex][j];
                    }
                }
                // console.log(minIndex);
                // console.log(minNode);
                // console.log(minNode.getSiblingIndex());
                // console.log(minNode.zIndex);
                for (let i = 0; i < self.AllCorrectArray[blackIndex].length; i++) {
                    let blackNode = cc.instantiate(self.AllCorrectArray[blackIndex][i].getChildByName(self.AllCorrectArray[blackIndex][i].name));
                    let shadowx = self.AllCorrectArray[blackIndex][i].getChildByName(self.AllCorrectArray[blackIndex][i].name).x;
                    let shadowy = self.AllCorrectArray[blackIndex][i].getChildByName(self.AllCorrectArray[blackIndex][i].name).y;
                    let parentdeltax = self.AllCorrectArray[blackIndex][i].x - minNode.x;
                    let parentdeltay = self.AllCorrectArray[blackIndex][i].y - minNode.y;
                    // console.log(parentdeltax);
                    // console.log(parentdeltay);


                    blackNode.name = self.AllCorrectArray[blackIndex][i].name + "_s";
                    self.AllCorrectArray[blackIndex][i].addChild(blackNode);

                    blackNode.setSiblingIndex(0);
                    self.main.shadow.push(blackNode);
                    blackNode.color = cc.color(0, 0, 0, 255);
                    blackNode.opacity = 160;


                    blackNode.parent = minNode;
                    blackNode.setSiblingIndex(0);
                    if (this.rotation % 360 == 0) {
                        blackNode.setPosition(cc.v2(shadowx + parentdeltax + 5, shadowy + parentdeltay - 5));
                    }
                    else if (this.rotation % 360 == 90) {
                        blackNode.setPosition(cc.v2(shadowx - parentdeltay + 5, shadowy + parentdeltax + 5));
                    }
                    else if (this.rotation % 360 == 180) {
                        blackNode.setPosition(cc.v2(shadowx - parentdeltax - 5, shadowy - parentdeltay + 5));
                    }
                    else if (this.rotation % 360 == 270) {
                        blackNode.setPosition(cc.v2(shadowx + parentdeltay - 5, shadowy - parentdeltax - 5));
                    }


                    //  blackNode.setPosition(cc.v2(blackNode.x + parentdeltax, blackNode.y + parentdeltay))
                    //    console.log(i);
                    //  console.log(blackNode.parent);


                }
            }
            else {
                let blackNode = cc.instantiate(this.getChildByName(this.name));
                let shadowx = this.getChildByName(this.name).x;
                let shadowy = this.getChildByName(this.name).y;
                blackNode.name = this.name + "_s";
                //  console.log(blackNode);
                this.addChild(blackNode);
                blackNode.setSiblingIndex(0);
                self.main.shadow.push(blackNode);
                // blackNode.rotation = this.rotation;
                blackNode.color = cc.color(0, 0, 0, 255);
                blackNode.opacity = 160;
                //blackNode.setPosition(cc.v2(this.x + 5, this.y - 5));
                if (this.rotation % 360 == 0) {
                    blackNode.setPosition(cc.v2(shadowx + 5, shadowy - 5));
                }
                else if (this.rotation % 360 == 90) {
                    blackNode.setPosition(cc.v2(shadowx + 5, shadowy + 5));
                }
                else if (this.rotation % 360 == 180) {
                    blackNode.setPosition(cc.v2(shadowx - 5, shadowy + 5));
                }
                else if (this.rotation % 360 == 270) {
                    blackNode.setPosition(cc.v2(shadowx - 5, shadowy - 5));
                }


            }

        }, this.node);
        self.backIndex = -1;
        this.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
            if (self.out) {
                self.main.scrollView.getComponent(cc.ScrollView).enabled = false;
                var delta = event.touch.getDelta();
                self.only = false;
                this.opacity = 255;
                this.x += delta.x;
                this.y += delta.y;
            } else {
                var delta = event.touch.getDelta();

                if (self.only) {

                    if (Math.abs(delta.y / delta.x) < 1) {
                        self.isDrag = false;
                    }
                    else {
                        self.isDrag = true;
                        //this.position = cc.find("Canvas/MovePart").convertToNodeSpaceAR(event.getLocation());
                    }
                }
                if (self.isDrag == true) {
                    self.main.scrollView.getComponent(cc.ScrollView).enabled = false;

                    self.only = false;
                    this.opacity = 255;
                    this.x += delta.x;
                    this.y += delta.y;
                }
                else {
                    self.only = false;
                    //  self.isSlide = true;
                    this.x += 0;
                    this.y += 0;
                }
            }
            //  console.log(self.isDrag);
            if (self.isDrag == true) {
                // for (let i = 0; i < selfmainmain.shadow.length; i++) {
                //     selfmainmain.shadow[i].x += delta.x;
                //     selfmainmain.shadow[i].y += delta.y;

                // }
                //拖拽前后，下面方块的变化(不松手)

                if (self.AllCorrectArray.length > 0) {


                    //遍历连接正确的数组，找到点击物体所在的数组
                    for (let i = 0; i < self.AllCorrectArray.length; i++) {
                        for (let j = 0; j < self.AllCorrectArray[i].length; j++) {
                            if (self.AllCorrectArray[i].indexOf(this) != -1 && j != self.AllCorrectArray[i].indexOf(this)) {
                                self.AllCorrectArray[i][j].x += delta.x;
                                self.AllCorrectArray[i][j].y += delta.y;

                            }
                        }

                    }

                }


                //console.log(self.AllCorrectArray);
                if (this.y >= 40) {


                    if (self.once == false) {
                        return;
                    }
                    self.once = false;
                    if (this.parent != self.main.movePart) {
                        this.parent = self.main.movePart;
                        // console.log(this.parent);
                        // console.log(self.main.content);
                        this.position = cc.v2(this.position.x + self.main.content.position.x, this.position.y);
                        //console.log(self.startpuzzle);
                    }


                    for (let i = self.startpuzzle.indexOf(this) + 1; i < self.startpuzzle.length; i++) {
                        self.startpuzzle[i].stopAllActions();
                        var action = cc.moveTo(0.15, self.startpos[i - 1]);
                        //console.log("原始位置" + this.position);
                        self.startpuzzle[i].runAction(action);
                        // console.log("向前一步走");
                        //console.log("结束位置" + self.startpos[i - 1]);
                    }
                    // console.log(self.mousePos0.x);
                    // console.log(self.startpuzzle.indexOf(this));
                    // console.log( self.main.content.childrenCount);
                    //  console.log(self.startpuzzle.indexOf(this));
                    //  console.log(self.main.content.childrenCount);
                    //  console.log(self.mousePos0.x);

                    if (self.startpuzzle.indexOf(this) > 3 && self.startpuzzle.indexOf(this) < self.main.content.childrenCount - 3) {
                        if (self.mousePos0.x > 360) {
                            var contentAction = cc.moveBy(0.15, cc.v2(200, 0));
                            self.main.content.runAction(contentAction);
                        }

                    }
                    else if (self.startpuzzle.indexOf(this) >= self.main.content.childrenCount - 3 && self.startpuzzle.indexOf(this) > 3) {
                        // if (self.mousePos0.x <= 360) {
                        var contentAction = cc.moveBy(0.15, cc.v2(200, 0));
                        self.main.content.runAction(contentAction);
                        // }

                    }
                    else if (self.startpuzzle.indexOf(this) <= 3 && self.startpuzzle.indexOf(this) >= self.main.content.childrenCount - 3) {
                        if (self.main.content.childrenCount == 4) {
                            if (self.startpuzzle.indexOf(this) == 1) {
                                if (self.mousePos0.x < 180) {
                                    var contentAction = cc.moveBy(0.15, cc.v2(200, 0));
                                    self.main.content.runAction(contentAction);
                                }
                            }
                            else if (self.startpuzzle.indexOf(this) == 2) {
                                if (self.mousePos0.x < 360) {
                                    var contentAction = cc.moveBy(0.15, cc.v2(200, 0));
                                    self.main.content.runAction(contentAction);
                                }
                            }
                            else if (self.startpuzzle.indexOf(this) == 3) {
                                if (self.mousePos0.x < 540) {
                                    var contentAction = cc.moveBy(0.15, cc.v2(200, 0));
                                    self.main.content.runAction(contentAction);
                                }
                            }


                        }
                        else {
                            if (self.main.content.childrenCount > 4) {
                                if (self.mousePos0.x < 360) {
                                    var contentAction = cc.moveBy(0.15, cc.v2(200, 0));
                                    self.main.content.runAction(contentAction);
                                }
                            }
                        }


                    }
                    //  self.isHave[self.startpuzzle.indexOf(this)] = true;
                    //  self.isHave[self.startpuzzle.length - 1] = false;
                    self.startpuzzle.splice(self.startpuzzle.indexOf(this), 1);
                    if (self.main.content.getChildren().length > 4) {
                        self.main.content.width -= 200;
                    }
                    // console.log(self.main.content.width);

                    // console.log("减");
                    // console.log(self.main.content.width);
                    // for (let i = 0; i < self.startpuzzle.length; i++) {

                    // }




                    // console.log(self.isHave);
                    // console.log(self.startpuzzle);
                    // console.log("删一个");

                }
                else if (this.y < 40) {
                    for (let i = 0; i < self.AllCorrectArray.length; i++) {
                        for (let j = 0; j < self.AllCorrectArray[i].length; j++) {
                            if (self.AllCorrectArray[i].indexOf(this) != -1) {
                                return;
                            }
                        }
                    }


                    var pos = this.parent.convertToWorldSpaceAR(this.position);

                    // console.log(pos);
                    if (self.once == false) {

                        self.once = true;
                        //   if (self.main.slideOver) {
                        this.parent = self.main.content;
                        // self.main.slideOver = false;
                        //   }

                        //  console.log(this.parent);

                        this.position = cc.v2(this.position.x - self.main.content.position.x, this.position.y);
                        //  if (self.main.content.getChildren().length > 4) {
                        for (let i = 0; i < self.startpuzzle.length; i++) {
                            //console.log("294呢");
                            //console.log(self.startpuzzle[i].parent.convertToWorldSpaceAR(self.startpuzzle[i].position));
                            if (self.startpuzzle[i].parent.convertToWorldSpaceAR(self.startpuzzle[i].position).x > pos.x) {
                                // console.log(i);
                                self.haveBig = true;
                                self.backIndex = i;
                                for (let j = i; j < self.startpuzzle.length; j++) {
                                    // self.startpuzzle[j].position = self.startpos[j + 1];
                                    self.startpuzzle[j].stopAllActions();
                                    var action = cc.moveTo(0.15, self.startpos[j + 1]);

                                    self.startpuzzle[j].runAction(action);

                                }
                                self.main.content.stopAllActions();
                                self.startpuzzle.splice(i, 0, this);
                                if (self.main.content.getChildren().length > 5) {
                                    self.main.content.width += 200;
                                }
                                //console.log(self.main.content.width);
                                //  console.log("加");
                                //  console.log(self.startpuzzle);
                                // self.isHave[i] = false;
                                // self.isHave[self.startpuzzle.length - 1] = true;
                                // console.log(self.isHave);

                                break;
                            }
                            else {
                                self.haveBig = false;
                            }

                        }


                        if (self.haveBig == false) {
                            self.startpuzzle.push(this);
                            self.backIndex = self.startpuzzle.length - 1;
                            //  console.log(self.backIndex);
                            if (self.main.content.getChildren().length > 5) {
                                self.main.content.width += 200;
                            }
                            // console.log(self.isHave);
                            // console.log(self.startpuzzle);
                        }
                        //   }



                    }
                    else {

                        //  console.log("是不走这来了");
                        // console.log(self.isHave);
                        //  if (self.main.slideOver) {
                        this.parent = self.main.content;
                        //    self.main.slideOver = false;
                        //  }



                        //this.position = cc.v2(this.position.x - cc.find("Canvas/PuzzleChooseScrollView/view/content").position.x, this.position.y);

                        if (self.startpuzzle.indexOf(this) != -1) {
                            self.backIndex = self.startpuzzle.indexOf(this);
                            // console.log(self.backIndex);
                        }

                        for (let i = 0; i < self.startpuzzle.length; i++) {
                            if (i == self.startpuzzle.indexOf(this)) {
                                continue;
                            }
                            //  if (i < 3) {
                            // console.log(pos);
                            //  console.log(self.startpuzzle[i]);

                            //   console.log(self.startpuzzle[i].position.x);
                            //  console.log(Math.abs(pos.x - self.startpuzzle[i].parent.convertToWorldSpaceAR(self.startpuzzle[i].position).x));
                            //   }


                            if (Math.abs(pos.x - self.startpuzzle[i].parent.convertToWorldSpaceAR(self.startpuzzle[i].position).x) <= 30) {

                                // for (let j = 0; j < self.isHave.length; j++) {
                                // if (self.isHave[j] == false) {
                                // console.log(self.backIndex);
                                //  console.log(i);
                                //  console.log(self.startpuzzle[i]);
                                //  console.log(self.startpuzzle[i].position.x.toString());
                                //  console.log(self.startpuzzle[i].position.y.toString());
                                //  console.log(self.startpos[self.backIndex].x.toString());
                                //  console.log(self.startpos[self.backIndex].y.toString());
                                // if (self.startpuzzle[i].x > this.x) {
                                //     self.startpuzzle[i].setPosition(cc.v2(self.startpuzzle[i].x - 200, self.startpuzzle[i].y));
                                // }
                                // else {
                                //     self.startpuzzle[i].setPosition(cc.v2(self.startpuzzle[i].x + 200, self.startpuzzle[i].y));
                                // }
                                self.startpuzzle[i].stopAllActions();
                                self.startpuzzle[i].setPosition(self.startpos[self.backIndex]);
                                // console.log(self.startpuzzle[i]);
                                // console.log(self.startpuzzle[i].position);
                                var bottle = null;
                                var bottle2 = null;

                                //  self.isHave[self.startpuzzle.indexOf(this)] = true;
                                // self.isHave[self.startpuzzle.indexOf(self.startpuzzle[i])] = false;
                                bottle = self.startpuzzle[self.startpuzzle.indexOf(this)];
                                //  console.log(bottle);
                                //  console.log(self.startpuzzle[i]);
                                // console.log(self.startpuzzle[self.startpuzzle.indexOf(this)]);

                                self.startpuzzle[self.startpuzzle.indexOf(this)] = self.startpuzzle[i];
                                //console.log(bottle);
                                // console.log(self.startpuzzle[i]);
                                // console.log(self.startpuzzle[self.startpuzzle.indexOf(this)]);

                                self.startpuzzle[i] = bottle;
                                self.backIndex = i;
                                // console.log(bottle);
                                //console.log(self.startpuzzle[i]);
                                //console.log(self.startpuzzle[self.startpuzzle.indexOf(this)]);

                                // console.log(self.backIndex);
                                //  console.log(self.startpuzzle);
                                break;
                                // }
                                // }
                            }
                        }

                    }




                }
            }


        }, this.node);
        this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
            //.log("进end");
            if (this.y < 40) {
                self.main.dontOnMask.parent = null;
                self.main.donContOnMask.parent = null;
            }

            if (self.dontTwoEnd == false) {
                return;
            }
            self.dontTwoEnd = false;


            for (let i = 0; i < self.main.shadow.length; i++) {
                self.main.shadow[i].destroy();
            }
            self.main.shadow.splice(0, self.main.shadow.length);

            var wssss = event.getLocation();

            var nodePos = self.main.puzzleBGNode.convertToNodeSpaceAR(wssss);

            //console.log(nodePos);

            self.only = true;
            clearTimeout(self.poschangedelaytime);

            // self.letgo = false;


            // self.poschangedelaytime = setTimeout(() => {
            //     self.letgo = true;
            // }, 160);
            // self.JudgeAnimation();
            self.JudgeSort();
            if (Math.floor(self.mousePos0.x) == Math.floor(wssss.x) && Math.floor(self.mousePos0.y) == Math.floor(wssss.y)) {
                if (nodePos.x < -360 || nodePos.x > 360 || nodePos.y > 960 || nodePos.y < 0) {
                    //console.log("rui了");
                    return;

                }
                if (this.parent == self.main.content) {
                    return;
                }
                if (cc.find("root").getComponent(Root).isRotate == true) {
                    this.rotation += 90;
                    //console.log("转");
                    if (self.AllCorrectArray.length > 0) {

                        var index = -1;
                        //遍历连接正确的数组，找到点击物体所在的数组
                        for (let i = 0; i < self.AllCorrectArray.length; i++) {
                            for (let j = 0; j < self.AllCorrectArray[i].length; j++) {
                                if (this == self.AllCorrectArray[i][j]) {
                                    index = i;
                                    break;
                                }
                            }
                            if (index != -1) {
                                break;
                            }
                        }
                        if (index != -1) {
                            //找到以后，
                            for (let i = 0; i < self.AllCorrectArray[index].length; i++) {
                                if (i == self.AllCorrectArray[index].indexOf(this)) {
                                    continue;
                                }

                                self.distance = Math.sqrt((this.x - self.AllCorrectArray[index][i].x) * (this.x - self.AllCorrectArray[index][i].x) + (this.y - self.AllCorrectArray[index][i].y) * (this.y - self.AllCorrectArray[index][i].y))
                                if (self.AllCorrectArray[index][i].x >= this.x) {
                                    self.AllCorrectArray[index][i].position = cc.v2(Math.cos(Math.asin((self.AllCorrectArray[index][i].y - this.y) / self.distance) - Math.PI / 2) * self.distance + this.x, Math.sin(Math.asin((self.AllCorrectArray[index][i].y - this.y) / self.distance) - Math.PI / 2) * self.distance + this.y);
                                }
                                else {
                                    self.AllCorrectArray[index][i].position = cc.v2(Math.cos(- Math.asin((self.AllCorrectArray[index][i].y - this.y) / self.distance) + Math.PI / 2) * self.distance + this.x, Math.sin(-Math.asin((self.AllCorrectArray[index][i].y - this.y) / self.distance) + Math.PI / 2) * self.distance + this.y);
                                }

                                self.AllCorrectArray[index][i].rotation += 90;

                                //console.log(self.AllCorrectArray[index][i].position);
                            }
                        }




                    }
                }

                if (self.main.mask.active == false) {
                    self.main.mask.active = true;

                }
                setTimeout(() => {
                    self.main.mask.active = false;
                }, 200);
            }



            self.main.scrollView.getComponent(cc.ScrollView).enabled = true;

            if (this.y >= 40) {

                self.up80(nodePos);



            }
            else if (this.y < 40) {
                var shoodreturn = false;
                for (let i = 0; i < self.AllCorrectArray.length; i++) {
                    for (let j = 0; j < self.AllCorrectArray[i].length; j++) {
                        if (self.AllCorrectArray[i].indexOf(this) != -1) {
                            shoodreturn = true;
                            self.up80(nodePos);
                            break;
                        }
                    }
                    if (shoodreturn) {
                        return;
                    }
                }

                self.out = false;



                // for (let i = 0; i < self.isHave.length; i++) {
                // if (self.isHave[i] == false) {
                //console.log("动一动");
                var action2 = cc.moveTo(0.1, self.startpos[self.backIndex]);
                this.runAction(action2);
                // self.isHave[i] = true;
                // console.log(self.backIndex);
                // console.log(self.startpuzzle);
                //  break;
                // }

                //}

            }


        }, this.node);

        this.node.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
            // console.log("console");
            // if (!self.main.scrollView.isScrolling) {
            //     for (let i = 0; i < self.main.shadow.length; i++) {
            //         self.main.shadow[i].destroy();
            //     }
            //     self.main.shadow.splice(0, self.main.shadow.length);
            // }


            // let blackIndex3 = -1;
            // for (let i = 0; i < self.AllCorrectArray.length; i++) {
            //     if (self.AllCorrectArray[i].indexOf(this) != -1) {
            //         blackIndex2 = i;
            //         break;
            //     }
            // }
            // if (blackIndex3 != -1) {
            //     for (let i = 0; i < self.AllCorrectArray[blackIndex3.length]; i++) {
            //         if (self.AllCorrectArray[blackIndex3][i].getChildByName(self.AllCorrectArray[blackIndex3][i].name + "_s") != null) {
            //             self.AllCorrectArray[blackIndex3][i].getChildByName(self.AllCorrectArray[blackIndex3][i].name + "_s").destroy();
            //         }

            //     }
            // }
            // else {
            //     if (this.getChildByName(this.name + "_s") != null) {
            //         this.getChildByName(this.name + "_s").destroy();
            //     }

            // }
            //  console.log("进cancel");

            self.only = true;
            // self.main.dontOnMask.parent = null;
            // self.main.donContOnMask.parent = null;



            self.main.scrollView.getComponent(cc.ScrollView).enabled = true;


        }, this.node);

        this.node.on('position-changed', function () {
            self.posChange();
        }, this.node);
    },

    start() {

    },
    up80(nodePos) {
        var self = this;
        self.out = true;

        //console.log("一上来进了吧");
        for (let i = 0; i < self.puzzleBG.length; i++) {
            var dis = Math.sqrt((nodePos.x - self.puzzleBG[i].x) * (nodePos.x - self.puzzleBG[i].x) + (nodePos.y - self.puzzleBG[i].y) * (nodePos.y - self.puzzleBG[i].y))
            self.disArray.push(dis);
        }
        //console.log(self.disArray);
        var min = self.disArray[0];
        for (; self.index < self.disArray.length; self.index++) {
            if (self.disArray[self.index] <= min) {
                min = self.disArray[self.index];
                self.minIndex = self.index;
            }
        }
        //console.log(self.puzzleBG);
        self.BGW = self.puzzleBG[self.minIndex].name[0];
        self.BGH = self.puzzleBG[self.minIndex].name[2];
        //console.log(self.BGW + self.BGH);
        let myfriends = [];
        let isFinded = false;

        for (let i = 0; i < self.AllCorrectArray.length; i++) {
            if (self.AllCorrectArray[i].indexOf(this.node) != -1) {
                for (let j = 0; j < self.AllCorrectArray[i].length; j++) {
                    myfriends.push(self.AllCorrectArray[i][j]);

                }
                isFinded = true;
            }
            if (isFinded) {
                break;
            }
        }
        //this.node.position = self.puzzleBG[self.minIndex];
        var finishedMain = cc.callFunc(function (target) {
            self.main.dontOnMask.parent = null;
            self.main.donContOnMask.parent = null;
            // console.log(this.node);
            // console.log(this.node.position);
            self.letgo = true;
            self.moveMusic.play();
            let haveChange = self.JudgeIsHave(self.BGW, self.BGH);
            self.JudgeIsLock();


            setTimeout(() => {
                // self.JudgeAnimation();
                self.main.animNum.splice(0, self.main.animNum.length);
                self.JudgeSort();

                // console.log("走几遍啊兄弟");
                let index = -1;
                let lightArray = [];
                for (let i = 0; i < self.AllCorrectArray.length; i++) {
                    if (self.AllCorrectArray[i].indexOf(this.node) != -1) {
                        index = i;
                        break;
                    }
                }
                if (haveChange) {
                    if (index != -1) {
                        // console.log(self.AllCorrectArray);
                        for (let i = 0; i < self.AllCorrectArray[index].length; i++) {
                            //  console.log(self.AllCorrectArray[index][i].position.x);
                            //   console.log(self.AllCorrectArray[index][i].position.y);
                            //   console.log(this.node.x);
                            //   console.log(this.node.y);

                            //  console.log(this.node);
                            // console.log(self.AllCorrectArray[index][i]);
                            // console.log(Math.sqrt((self.AllCorrectArray[index][i].position.x - this.node.x) * (self.AllCorrectArray[index][i].position.x - this.node.x) + (self.AllCorrectArray[index][i].position.y - this.node.y) * (self.AllCorrectArray[index][i].position.y - this.node.y)));
                            if (Math.sqrt((self.AllCorrectArray[index][i].position.x - this.node.x) * (self.AllCorrectArray[index][i].position.x - this.node.x) + (self.AllCorrectArray[index][i].position.y - this.node.y) * (self.AllCorrectArray[index][i].position.y - this.node.y)) < 150) {

                                lightArray.push(self.AllCorrectArray[index][i]);
                                // console.log("pushadkjfgaslkjglksajglskjg");
                            }

                        }
                        // console.log(lightArray);
                        self.JudgeIsLight(lightArray);
                    }

                }


                for (let i = 0; i < self.AllCorrectArray.length; i++) {
                    if (self.AllCorrectArray[i].length == self.width * self.height && self.BGW == this.name[0] && self.BGH == this.name[2] && this.node.rotation % 360 == 0) {
                        self.gameOver();
                    }
                }
                // console.log(self.AllCorrectArray);

                // console.log(self.main.movePart);

            }, 0);






        }, this)
        // console.log(self.minIndex);
        //  console.log(self.puzzleBG[self.minIndex]);

        var actionMain = cc.moveTo(0.1, self.puzzleBG[self.minIndex]);



        var action2Main = cc.sequence(actionMain, finishedMain);
        // console.log(self.isHave);
        var sbaction = this.node.runAction(action2Main);
        // console.log(sbaction);

        for (let j = 0; j < myfriends.length; j++) {

            if (j == myfriends.indexOf(this.node)) {
                continue;
            }
            let comparePos = cc.v2((self.puzzleBG[self.minIndex].x) + (myfriends[j].x - this.node.x), (self.puzzleBG[self.minIndex].y) + (myfriends[j].y - this.node.y));


            let action = cc.moveTo(0.1, comparePos);
            let finished = cc.callFunc(function (target) {
                // console.log("回调了");
                // console.log("小兄弟们一起判断");

                // let haveChangefriends = self.JudgeIsHave(self.BGWFriend, self.BGHFriend);
                if (comparePos.x >= -360 && comparePos.x <= 360 && comparePos.y <= 960 && comparePos.y >= 0) {

                    for (let k = 0; k < self.puzzleBG.length; k++) {
                        var disFriend = Math.sqrt((comparePos.x - self.puzzleBG[k].x) * (comparePos.x - self.puzzleBG[k].x) + (comparePos.y - self.puzzleBG[k].y) * (comparePos.y - self.puzzleBG[k].y))
                        self.disArrayFriend.push(disFriend);
                    }
                    let min = self.disArrayFriend[0];
                    for (; self.indexFriend < self.disArrayFriend.length; self.indexFriend++) {
                        if (self.disArrayFriend[self.indexFriend] <= min) {
                            min = self.disArrayFriend[self.indexFriend];
                            self.minIndexFriend = self.indexFriend;
                        }
                    }

                    self.BGWFriend = self.puzzleBG[self.minIndexFriend].name[0];
                    self.BGHFriend = self.puzzleBG[self.minIndexFriend].name[2];
                    //console.log(self.BGWFriend + self.BGHFriend);
                    // console.log(self.AllCorrectArray);
                    //console.log(i + ",," + j);
                    let friendDrag = myfriends[j].getComponent("Drag");
                    let haveChangefriends = friendDrag.JudgeIsHave(self.BGWFriend, self.BGHFriend);

                    friendDrag.JudgeIsLock();

                    friendDrag.letgo = true;
                    //console.log("小兄弟进了");

                    let indexfriends = -1;
                    let lightArrayfriends = [];
                    for (let i = 0; i < self.AllCorrectArray.length; i++) {
                        if (self.AllCorrectArray[i].indexOf(myfriends[j]) != -1) {
                            indexfriends = i;
                            break;
                        }
                    }
                    if (haveChangefriends) {
                        //if (index != -1) {
                        // console.log(self.AllCorrectArray);
                        for (let i = 0; i < self.AllCorrectArray[indexfriends].length; i++) {
                            //  console.log(self.AllCorrectArray[index][i].position.x);
                            //   console.log(self.AllCorrectArray[index][i].position.y);
                            //   console.log(this.node.x);
                            //   console.log(this.node.y);

                            //  console.log(this.node);
                            // console.log(self.AllCorrectArray[index][i]);
                            // console.log(Math.sqrt((self.AllCorrectArray[index][i].position.x - this.node.x) * (self.AllCorrectArray[index][i].position.x - this.node.x) + (self.AllCorrectArray[index][i].position.y - this.node.y) * (self.AllCorrectArray[index][i].position.y - this.node.y)));
                            if (Math.sqrt((self.AllCorrectArray[indexfriends][i].position.x - myfriends[j].x) * (self.AllCorrectArray[indexfriends][i].position.x - myfriends[j].x) + (self.AllCorrectArray[indexfriends][i].position.y - myfriends[j].y) * (self.AllCorrectArray[indexfriends][i].position.y - myfriends[j].y)) < 150) {

                                lightArrayfriends.push(self.AllCorrectArray[indexfriends][i]);
                                // console.log("pushadkjfgaslkjglksajglskjg");
                            }

                        }
                        // console.log(lightArray);
                        self.JudgeIsLight(lightArrayfriends);
                        // }

                    }

                }



                self.disArrayFriend.splice(0, self.puzzleBG.length);
                self.indexFriend = 1;
                self.minIndexFriend = 0;


            }, this);
            var action2 = cc.sequence(action, finished);
            myfriends[j].runAction(action2);

        }




        self.disArray.splice(0, self.puzzleBG.length);
        self.index = 1;
        self.minIndex = 0;

    },
    gameOver() {
        var self = this;
        for (let i = 0; i < cc.animPos[0].length; i++) {
            cc.animPos[0][i].animActive = false;
            cc.animPos[0][i].findNum = 0;
            cc.animPos[0][i].animPrefab = null;
        }



        //this.main.spine.parent = this.main.lockPart;
        // this.main.spine.position = cc.v2(this.main.spine.position.x, this.main.spine.position.y + 520);
        this.main.completePic.parent = this.main.lockPart;
        this.main.completePic.position = cc.v2(this.main.completePic.position.x, this.main.completePic.position.y + 520);


        this.main.mask.active = true;
        this.main.contentMask.active = true;
        //this.main.spine.active = true;
        // let spineMagic = this.main.spine.getComponent(sp.Skeleton);
        // let yellowbig = cc.scaleTo(1, 1);
        // this.main.yellow.runAction(yellowbig);
        // let blackbig = cc.scaleTo(1, 1);
        // let delay = cc.delayTime(0.25);
        // let large = cc.sequence(delay, blackbig);
        // this.main.black.runAction(large);
        setTimeout(() => {
            //spineMagic.setAnimation(0, '1', true);
            // self.main.spine.setSiblingIndex(0);
            self.main.completePic.setSiblingIndex(0);
            // console.log("zindex="+self.main.spine.zIndex);
            let children = self.main.lockPart.getChildren();
            let children2 = self.main.movePart.getChildren();
            //  console.log(children);
            // console.log("zindex2="+self.main.spine.zIndex);
            for (let i = 0; i < children2.length; i++) {
                // if (children[i].name == "magic") {
                //     continue;
                // }
                children2[i].getChildByName(children2[i].name).getComponent(cc.Sprite).spriteFrame = null;
            }

            for (let i = 1; i < children.length; i++) {

                children[i].getChildByName(children[i].name).getComponent(cc.Sprite).spriteFrame = null;
            }
            for (let i = 0; i < self.main.curanim.length; i++) {
                self.main.curanim[i].getChildByName("line").active = false;
            }
            // self.main.spine.active = true;
            self.main.completePic.active = true;
            //spineMagic.timeScale = 1;


        }, 0);
        setTimeout(() => {
            self.main.repeatLabel.active = true;
            self.main.repeatButton.active = true;
            let action = cc.scaleTo(0.5, 1, 1);
            self.main.winLabel.runAction(action);
        }, 750);

    },
    posChange() {
        var self = this;
        if (self.letgo && this.node.y > 40) {
            for (let i = 0; i < self.BGisHave.length; i++) {
                for (let j = 0; j < self.BGisHave[i].length; j++) {
                    for (let k = 0; k < self.BGisHave[i][j].length; k++) {
                        if (this.node == self.BGisHave[i][j][k]) {
                            self.BGisHave[i][j].splice(k, 1);
                            // console.log("pop掉");
                            // console.log(self.BGisHave);
                            self.isfind = true;
                            break;
                        }
                    }
                    if (self.isfind == true) {
                        break;
                    }
                }
                if (self.isfind == true) {
                    self.isfind = false;
                    break;
                }
            }
            self.letgo = false;
        }
    },
    JudgeAnimation() {
        var self = this;



        let indexAnim = 0;
        // let animAction = 0;


        //  console.log(self.AllCorrectArray);
        if (self.AllCorrectArray.length > 0) {
            //   console.log("大了");
            // console.log(self.AllCorrectArray);
            // console.log(indexAnim);
            //  console.log(self.main.animNum);
            if (self.main.animNum.length > 0) {
                //  console.log("也大了");
                for (let i = 0; i < self.AllCorrectArray.length; i++) {

                    if (self.AllCorrectArray[i].indexOf(this.node) != -1) {
                        indexAnim = i;
                        break;
                    }


                }
                // console.log(indexAnim);
                for (let k = 0; k < self.main.animNum.length; k++) {

                    for (let i = 0; i < self.AllCorrectArray[indexAnim].length; i++) {

                        for (let j = 0; j < cc.animPos[0][self.main.animNum[k]].bg.length; j++) {

                            // console.log("大于0");

                            //  console.log(self.AllCorrectArray[indexAnim]);
                            if (self.AllCorrectArray[indexAnim][i].name == cc.animPos[0][self.main.animNum[k]].bg[j]) {
                                //   console.log("都一样，直接出去");
                                if (cc.animPos[0][self.main.animNum[k]].findNum < cc.animPos[0][self.main.animNum[k]].count) {
                                    cc.animPos[0][self.main.animNum[k]].findNum++;
                                }
                                // animAction++;
                                break;
                            }


                        }
                        // }



                    }

                    // console.log(self.main.animNum[k]);
                    // console.log(cc.animPos[0][self.main.animNum[k]].findNum);
                    //  console.log(cc.animPos[0][self.main.animNum[k]].count);
                    if (cc.animPos[0][self.main.animNum[k]].findNum == cc.animPos[0][self.main.animNum[k]].count) {
                        //  console.log(cc.animPos[0][self.main.animNum].animActive);
                        if (cc.animPos[0][self.main.animNum[k]].animActive == false) {
                            //cc.loader.loadRes("Animation/round0/anim_" + self.main.animNum, cc.Prefab, function (err, anim) {
                            cc.animPos[0][self.main.animNum[k]].animPrefab = self.main.curanim[self.main.animNum[k]];
                            cc.animPos[0][self.main.animNum[k]].animPrefab.parent = self.node;




                            // console.log(cc.animPos[0][self.main.animNum].animPrefab);
                            cc.animPos[0][self.main.animNum[k]].animActive = true;


                            let offsetx = cc.animPos[0][self.main.animNum[k]].x - self.main.puzzleBGNode.getChildByName(self.name).x;
                            let offsety = cc.animPos[0][self.main.animNum[k]].y - self.main.puzzleBGNode.getChildByName(self.name).y;
                            //  console.log(offsetx + ",,,," + offsety);

                            cc.animPos[0][self.main.animNum[k]].animPrefab.setPosition(cc.v2(offsetx, offsety));
                            for (let i = 0; i < cc.animPos[0][self.main.animNum[k]].bg.length; i++) {
                                //  console.log(cc.animPos[0][self.main.animNum].bg);
                                // console.log("i=" + i);
                                let x = Number(cc.animPos[0][self.main.animNum[k]].bg[i]);

                                let newName = x + 6 * (8 - 2 * (Math.floor((x - 1) / 6) + 1) + 1);
                                // console.log("新名");
                                // console.log(newName);
                                if (self.main.movePart.getChildByName(cc.animPos[0][self.main.animNum[k]].bg[i]) != null) {
                                    self.main.movePart.getChildByName(cc.animPos[0][self.main.animNum[k]].bg[i]).getChildByName(cc.animPos[0][self.main.animNum[k]].bg[i]).getComponent(cc.Sprite).spriteFrame = cc.imgSplit.spriteAtlas.getSpriteFrame(newName + "_b");
                                }
                                else {
                                    self.main.lockPart.getChildByName(cc.animPos[0][self.main.animNum[k]].bg[i]).getChildByName(cc.animPos[0][self.main.animNum[k]].bg[i]).getComponent(cc.Sprite).spriteFrame = cc.imgSplit.spriteAtlas.getSpriteFrame(newName + "_b");
                                }

                            }




                            cc.animPos[0][self.main.animNum[k]].animPrefab.getComponent(sp.Skeleton).timeScale = 1;

                            //  console.log("动");



                            //});
                        }



                    }
                    else {
                        cc.animPos[0][self.main.animNum[k]].findNum = 0;

                    }
                }

            }





        }
        //  }


        // console.log(self.main.animNum);


    },
    JudgeSort() {
        var self = this;
        let box = [];
        for (let i = 0; i < self.AllCorrectArray.length; i++) {
            for (let j = 0; j < self.AllCorrectArray.length - 1; j++) {
                if (self.AllCorrectArray[j + 1].length <= self.AllCorrectArray[j].length) {
                    if (self.AllCorrectArray[j + 1].length == self.AllCorrectArray[j].length && self.AllCorrectArray[j + 1].indexOf(this.node) == -1) {
                        continue;
                    }

                    box = self.AllCorrectArray[j];
                    self.AllCorrectArray[j] = self.AllCorrectArray[j + 1];
                    self.AllCorrectArray[j + 1] = box;

                }

            }
        }
        //  console.log(self.AllCorrectArray);

        for (let i = 0; i < self.AllCorrectArray.length; i++) {
            let haveAnim = [];

            //   console.log(self.AllCorrectArray[i].indexOf(this.node));
            //if (self.AllCorrectArray[i].indexOf(this.node) != -1) {

            for (let j = 0; j < self.AllCorrectArray[i].length; j++) {
                //   console.log(self.AllCorrectArray[i][j].getChildren().length);
                // console.log(self.AllCorrectArray[i][j].getComponentInChildren(sp.Skeleton));
                if (self.AllCorrectArray[i][j].getComponentInChildren(sp.Skeleton) != null) {
                    haveAnim.push(self.AllCorrectArray[i][j]);

                    continue;
                }
                self.AllCorrectArray[i][j].setSiblingIndex(0);
                //  console.log(self.AllCorrectArray[i][j].name + "到最底下了");
                //  console.log("全放最下面");

            }
            if (haveAnim.length != 0) {
                // let allNum = 0;
                // for (let k = 0; k < self.AllCorrectArray.length; k++) {
                //     if (k == self.AllCorrectArray.indexOf(this.node)) {
                //         haveAnim.setSiblingIndex(self.AllCorrectArray[k].length - 1);

                //         break;
                //     }
                // }
                //  if (self.AllCorrectArray[i].indexOf(this.node) != -1) {
                // console.log(haveAnim);
                //console.log(self.AllCorrectArray[i].length - 1);
                for (let index = 0; index < haveAnim.length; index++) {
                    haveAnim[index].setSiblingIndex(self.AllCorrectArray[i].length - 1);

                }

                //log(haveAnim.name + "到第几层" + (self.AllCorrectArray[i].length - 1));


                //  }
            }

        }
    },
    JudgeIsLight(lightArray) {
        //console.log("亮");
        var self = this;

        for (let i = 0; i < lightArray.length; i++) {
            if (lightArray[i].getChildByName(lightArray[i].name + "_l") == null) {
                let light = new cc.Node(lightArray[i].name + "_l");
                light.zIndex = 1000;
                // console.log(light);
                lightArray[i].addChild(light);
                light.position = lightArray[i].getChildByName(lightArray[i].name).position;
                let x = Number(lightArray[i].name);
                // console.log(lightArray);
                // console.log(x);
                //console.log(self.main.column);
                // console.log(self.main.row);
                let newName = "";
                switch (cc.imgSplit.type) {
                    case 0:
                        newName = x + self.main.column * (self.main.row - 2 * (Math.floor((x - 1) / self.main.column) + 1) + 1);
                        
                        break;
                    case 1:
                        newName = self.main.row * self.main.column + 1 - x;
                        light.setScale(-1,1);
                        break;
                    case 2:
                        newName = x;
                        light.setScale(1,-1);
                        break;
                    case 3:
                        let temp = self.main.row * self.main.column + 1 - x;
                        newName = temp + self.main.column * (self.main.row - 2 * (Math.floor((temp - 1) / self.main.column) + 1) + 1);
                        light.setScale(-1,-1);
                        break;
                    default:
                        break;
                }
                let sprite = light.addComponent(cc.Sprite);
                if (self.main.column == 6) {
                    sprite.spriteFrame = self.main.lightAtlas_48.getSpriteFrame(newName);
                }
                else if (self.main.column == 4) {
                    // console.log("24亮组");
                    //console.log(newName);
                    sprite.spriteFrame = self.main.lightAtlas_24.getSpriteFrame(newName);
                }

                //  console.log(sprite.spriteFrame);
                light.opacity = 0;

                let Xian = cc.fadeIn(0.5);
                let Yin = cc.fadeOut(0.5);
                let finsh = cc.callFunc(function (target) {
                    light.destroy();
                }, this);
                let XianAndYin = cc.sequence(Xian, Yin, finsh);
                light.runAction(XianAndYin);
            }

        }

    },
    JudgeIsLock() {
        var self = this;
        console.log(this.node.rotation % 360);
        if (this.node.rotation % 360 == 0) {
            for (let i = 0; i < self.BGisHave.length; i++) {
                for (let j = 0; j < self.BGisHave[i].length; j++) {
                    if (self.BGisHave[i][j].length > 0) {
                        //console.log("上面有东西");
                        for (let m = 0; m < self.BGisHave[i][j].length; m++) {
                            //  console.log(self.BGisHave[i][j][m].getComponent("Drag").name);
                            //  console.log(self.main.puzzleBG2);
                            //  console.log(self.main.puzzleBG2[i][j].name);
                            if (self.BGisHave[i][j][m].getComponent("Drag").name == self.main.puzzleBG2[i][j].name && self.BGisHave[i][j][m].rotation % 360 == 0) {
                                //console.log("相等了");
                                self.main.isLock[i][j] = true;
                                break;
                            }
                            else {
                                self.main.isLock[i][j] = false;
                            }
                        }
                    }
                    else {
                        self.main.isLock[i][j] = false;
                    }

                }
            }
            //console.log(self.main.isLock);

            for (let i = 0; i < self.AllCorrectArray.length; i++) {
                // if (self.AllCorrectArray[i].indexOf(this.node) != -1) {
                if (self.AllCorrectArray[i].length >= self.height) {

                    for (let j = 0; j < self.AllCorrectArray[i].length; j++) {
                        let bgx = Math.floor((Number(self.AllCorrectArray[i][j].name) - 1) / self.main.column);
                        let bgy = Number((self.AllCorrectArray[i][j].name) - 1) % self.main.column;

                        if (self.main.isLock[bgx][bgy] == false) {
                            return;
                        }

                        console.log("到底关没关啊");
                        // console.log(self.AllCorrectArray[i][j].getComponent("Drag"));

                        self.AllCorrectArray[i][j].off(cc.Node.EventType.TOUCH_START);
                        self.AllCorrectArray[i][j].off(cc.Node.EventType.TOUCH_MOVE);
                        self.AllCorrectArray[i][j].off(cc.Node.EventType.TOUCH_END);
                        self.AllCorrectArray[i][j].off(cc.Node.EventType.TOUCH_CANCEL);// getComponent("Drag").enabled;
                        self.AllCorrectArray[i][j].parent = self.main.lockPart;
                        // if (self.LockArray.indexOf(self.AllCorrectArray[i][j]) == -1) {
                        //    self.LockArray.push(self.AllCorrectArray[i][j]);
                        // }
                        // console.log(self.AllCorrectArray);
                        // console.log(self.AllCorrectArray[i]);
                        // console.log(self.LockArray);
                    }
                }
                // if (self.main.LockArray.length == 0) {
                //     //console.log("赋值了");
                //     self.main.LockArray = self.AllCorrectArray[i];
                // }

                // break;
                //  }

                // }
                //console.log(self.isLock);
            }

            // var TopRowTrue = false;
            // var BottomRowTrue = false;
            // var LeftColumTrue = false;
            // var RightColumTrue = false;
            // for (var j = 0; j < self.isLock[0].length; j++) {
            //     if (self.isLock[0][j] == false) {
            //         BottomRowTrue = false;
            //         break;
            //     }
            //     else {
            //         BottomRowTrue = true;
            //     }
            // }
            // for (var j = 0; j < self.isLock[self.isLock.length - 1].length; j++) {
            //     if (self.isLock[self.isLock.length - 1][j] == false) {
            //         TopRowTrue = false;
            //         break;
            //     }
            //     else {
            //         TopRowTrue = true;
            //     }
            // }
            // for (var i = 0; i < self.isLock.length; i++) {
            //     //  for (var j = 0; j < self.isLock[i].length; j++) {
            //     if (self.isLock[i][0] == false) {
            //         LeftColumTrue = false;
            //         break;
            //     }
            //     else {
            //         LeftColumTrue = true;
            //     }
            //     //   }
            // }
            // for (var i = 0; i < self.isLock.length; i++) {
            //     // for (var j = 0; j < self.isLock[i].length; j++) {
            //     if (self.isLock[i][self.isLock[i].length - 1] == false) {
            //         RightColumTrue = false;
            //         break;
            //     }
            //     else {
            //         RightColumTrue = true;
            //     }
            //     // }
            // }

            //if ((TopRowTrue || BottomRowTrue) && (LeftColumTrue || RightColumTrue)) {


        }
    },
    JudgeIsHaveKernel(angele, target, numX, numY) {
        var self = this;
        let haveChange = false;
        //(target);
        if (target.length > 0) {
            // console.log("有人");

            for (let i = 0; i < target.length; i++) {
                // console.log(this.node);
                if (target[i].getComponent("Drag").name == (Number(this.name[0]) + numX) + "_" + (Number(this.name[2]) + numY)) {
                    //   console.log("对的人");
                    if (Math.floor(target[i].rotation % 360) == angele) {
                        //  console.log("又对了兄弟");
                        var isHaveArray = false;


                        var targetIn = false;
                        var targetAround = false;
                        for (let k = 0; k < self.AllCorrectArray.length; k++) {
                            for (let j = 0; j < self.AllCorrectArray[k].length; j++) {
                                if (self.AllCorrectArray[k][j] == this.node) {
                                    targetIn = true;
                                }
                                if (self.AllCorrectArray[k][j] == target[i]) {
                                    targetAround = true;
                                }
                            }
                        }
                        if (targetIn == false && targetAround == false) {
                            // console.log("都单");
                            self.AllCorrectArray.push([this.node, target[i]]);
                            //  let lightArray = [this.node, target[i]];
                            self.correctMusic.play();
                            haveChange = true;



                        }
                        else if (targetIn == false && targetAround == true) {
                            //console.log("我单");
                            for (let k = 0; k < self.AllCorrectArray.length; k++) {
                                for (let j = 0; j < self.AllCorrectArray[k].length; j++) {
                                    if (self.AllCorrectArray[k][j] == target[i]) {
                                        self.AllCorrectArray[k].push(this.node);
                                        // let lightArray = [this.node, target[i]];
                                        self.correctMusic.play();
                                        haveChange = true;


                                        break;
                                    }
                                }
                            }
                        }
                        else if (targetIn == true && targetAround == false) {
                            //  console.log("你单");
                            for (let k = 0; k < self.AllCorrectArray.length; k++) {
                                for (let j = 0; j < self.AllCorrectArray[k].length; j++) {
                                    if (self.AllCorrectArray[k][j] == this.node) {
                                        self.AllCorrectArray[k].push(target[i]);
                                        // let lightArray = self.AllCorrectArray[k];
                                        self.correctMusic.play();
                                        haveChange = true;


                                        break;
                                    }
                                }
                            }
                        }
                        else if (targetIn == true && targetAround == true) {
                            //   console.log("全有小兄弟");
                            var arrayIndex = 0;
                            for (let k = 0; k < self.AllCorrectArray.length; k++) {
                                for (let j = 0; j < self.AllCorrectArray[k].length; j++) {
                                    if (self.AllCorrectArray[k][j] == target[i]) {
                                        arrayIndex = k;
                                    }
                                }
                            }
                            //  console.log(arrayIndex);
                            var complete = false;

                            for (let k = 0; k < self.AllCorrectArray.length; k++) {
                                //  console.log("awwaa,," + this.node.name);
                                if (k == arrayIndex) {
                                    continue;
                                }
                                for (let j = 0; j < self.AllCorrectArray[k].length; j++) {
                                    //  console.log("sdfvvvv,," + this.node.name);

                                    if (self.AllCorrectArray[k][j] == this.node) {

                                        for (let m = 0; m < self.AllCorrectArray[k].length; m++) {
                                            //   console.log("ssccbttt,," + this.node.name);
                                            if (self.AllCorrectArray[arrayIndex].indexOf(self.AllCorrectArray[k][m]) == -1) {
                                                self.AllCorrectArray[arrayIndex].push(self.AllCorrectArray[k][m]);
                                                self.correctMusic.play();
                                                haveChange = true;

                                                //   console.log(self.AllCorrectArray[arrayIndex]);
                                            }
                                        }
                                        complete = true;
                                        //  setTimeout(() => {
                                        self.AllCorrectArray[k].splice(0, self.AllCorrectArray[k].length);
                                        //  }, 200);
                                        break;
                                    }
                                }
                                if (complete) {
                                    break;
                                }
                            }
                        }
                        //  console.log(self.AllCorrectArray);
                    }
                    break;
                }
            }
        }
        return haveChange;

    },
    JudgeIsHave(BGW, BGH) {
        //console.log(this.node.name + "判断");
        var self = this;
        let haveChangeUp = false;
        let haveChangeDown = false;
        let haveChangeLeft = false;
        let haveChangeRight = false;

        if (self.BGisHave[BGW - 1][BGH - 1].indexOf(this.node) == -1) {
            self.BGisHave[BGW - 1][BGH - 1].push(this.node);
            //  console.log("push了");
            //  console.log(self.BGisHave);
        }
        // console.log(BGW + BGH);
        //  console.log("height = " + self.height);
        //  console.log("width = " + self.width);
        //console.log(BGW + "," + BGH);
        let rot = this.node.rotation % 360;
        // console.log(360 - rot);
        // console.log(Math.round(Math.cos((360 - rot) * Math.PI / 180)));
        // console.log(Math.round(Math.sin((360 - rot) * Math.PI / 180)));
        // console.log("角度是：" + rot);
        // console.log(self.BGisHave);
        // console.log(self.BGisHave[BGW]);
        // console.log(BGW + "," + (BGH - 1));
        // console.log(self.BGisHave[BGW][BGH - 1]);
        // console.log((BGW - 1) + "," + (BGH - 2));
        // console.log(self.BGisHave[BGW - 1][BGH - 2]);
        // console.log((BGW - 1) + "," + BGH);
        // console.log(self.BGisHave[BGW - 1][BGH]);
        // console.log((BGW - 2) + "," + (BGH - 1));
        // console.log(self.BGisHave[BGW - 2][BGH - 1]);
        if (BGW != self.height) {
            //上面那块有东西
            //  console.log("上");
            haveChangeUp = self.JudgeIsHaveKernel(rot, self.BGisHave[BGW][BGH - 1], Math.round(Math.cos((360 - rot) * Math.PI / 180)), Math.round(Math.sin((360 - rot) * Math.PI / 180)));
        }
        if (BGH != 1) {
            //左面那块有东西
            // console.log("左");
            haveChangeLeft = self.JudgeIsHaveKernel(rot, self.BGisHave[BGW - 1][BGH - 2], Math.round(Math.sin((180 + rot) * Math.PI / 180)), Math.round(Math.cos((180 + rot) * Math.PI / 180)));
        }
        if (BGH != self.width) {
            //右面那块有东西
            //  console.log("右");
            haveChangeRight = self.JudgeIsHaveKernel(rot, self.BGisHave[BGW - 1][BGH], Math.round(Math.sin(rot * Math.PI / 180)), Math.round(Math.cos(rot * Math.PI / 180)));
        }
        if (BGW != 1) {
            //下面那块有东西
            // console.log("下");
            haveChangeDown = self.JudgeIsHaveKernel(rot, self.BGisHave[BGW - 2][BGH - 1], Math.round(Math.cos((180 - rot) * Math.PI / 180)), Math.round(Math.sin((180 - rot) * Math.PI / 180)));
        }
        if (haveChangeDown || haveChangeUp || haveChangeLeft || haveChangeRight) {
            return true;
        } else {
            return false;
        }

    },
    update(dt) {
    },
});
